/**
 * 
 */
package com.bolbachchan.blog.domain;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * @author Chaitanya
 * 
 */
@Entity
@Table(name = "POSTS")
@NamedQueries({
	@NamedQuery(name = "getPostsByUserId", query = "FROM Posts WHERE userDetails.objId= :userObjId"),
	@NamedQuery(name = "updatePost", query = "UPDATE Posts SET postTitle= :postTitle, postContent= :postContent, updateDt= :updateDt "
		+ "WHERE objId= :objId AND userDetails.objId= :userObjId") })
public class Posts implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 4610228928339288447L;

    private int objId;
    private UserDetails userDetails;
    private String postTitle;
    private String postContent;
    private Timestamp createDt;
    private Timestamp updateDt;

    private Set<Comments> comments = new HashSet<Comments>();

    /**
     * @return the objId
     */
    @Id
    @Column(name = "OBJ_ID", nullable = false)
    public int getObjId() {
	return objId;
    }

    /**
     * @param objId
     *            the objId to set
     */
    public void setObjId(int objId) {
	this.objId = objId;
    }

    /**
     * @return the userDetails
     */
    @ManyToOne(cascade = CascadeType.ALL, targetEntity = UserDetails.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "USER_OBJ_ID", referencedColumnName = "OBJ_ID")
    public UserDetails getUserDetails() {
	return userDetails;
    }

    /**
     * @param userDetails
     *            the userDetails to set
     */
    public void setUserDetails(UserDetails userDetails) {
	this.userDetails = userDetails;
    }

    /**
     * @return the postTitle
     */
    @Column(name = "POST_TITLE")
    public String getPostTitle() {
	return postTitle;
    }

    /**
     * @param postTitle
     *            the postTitle to set
     */
    public void setPostTitle(String postTitle) {
	this.postTitle = postTitle;
    }

    /**
     * @return the postContent
     */
    @Column(name = "POST_CONTENT")
    public String getPostContent() {
	return postContent;
    }

    /**
     * @param postContent
     *            the postContent to set
     */
    public void setPostContent(String postContent) {
	this.postContent = postContent;
    }

    /**
     * @return the createDt
     */
    @Column(name = "CREATE_DT")
    public Timestamp getCreateDt() {
	return createDt;
    }

    /**
     * @param createDt
     *            the createDt to set
     */
    public void setCreateDt(Timestamp createDt) {
	this.createDt = createDt;
    }

    /**
     * @return the updateDt
     */
    @Column(name = "UPDATE_DT")
    public Timestamp getUpdateDt() {
	return updateDt;
    }

    /**
     * @param updateDt
     *            the updateDt to set
     */
    public void setUpdateDt(Timestamp updateDt) {
	this.updateDt = updateDt;
    }

    /**
     * @return the comments
     */
    @OneToMany(mappedBy = "posts", fetch = FetchType.EAGER)
    public Set<Comments> getComments() {
	return comments;
    }

    /**
     * @param comments
     *            the comments to set
     */
    public void setComments(Set<Comments> comments) {
	this.comments = comments;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((comments == null) ? 0 : comments.hashCode());
	result = prime * result + ((createDt == null) ? 0 : createDt.hashCode());
	result = prime * result + objId;
	result = prime * result + ((postContent == null) ? 0 : postContent.hashCode());
	result = prime * result + ((postTitle == null) ? 0 : postTitle.hashCode());
	result = prime * result + ((updateDt == null) ? 0 : updateDt.hashCode());
	result = prime * result + ((userDetails == null) ? 0 : userDetails.hashCode());
	return result;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	Posts other = (Posts) obj;
	if (comments == null) {
	    if (other.comments != null)
		return false;
	}
	else if (!comments.equals(other.comments))
	    return false;
	if (createDt == null) {
	    if (other.createDt != null)
		return false;
	}
	else if (!createDt.equals(other.createDt))
	    return false;
	if (objId != other.objId)
	    return false;
	if (postContent == null) {
	    if (other.postContent != null)
		return false;
	}
	else if (!postContent.equals(other.postContent))
	    return false;
	if (postTitle == null) {
	    if (other.postTitle != null)
		return false;
	}
	else if (!postTitle.equals(other.postTitle))
	    return false;
	if (updateDt == null) {
	    if (other.updateDt != null)
		return false;
	}
	else if (!updateDt.equals(other.updateDt))
	    return false;
	if (userDetails == null) {
	    if (other.userDetails != null)
		return false;
	}
	else if (!userDetails.equals(other.userDetails))
	    return false;
	return true;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
	return "Posts [objId=" + objId + ", userDetails=" + userDetails + ", postTitle=" + postTitle + ", postContent="
		+ postContent + ", createDt=" + createDt + ", updateDt=" + updateDt + ", comments=" + comments + "]";
    }
}
